<?php

namespace addons\fastchat;

use think\Addons;
use think\Console;
use app\common\library\Menu;

/**
 * 插件
 */
class Fastchat extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        // 创建菜单
        $menu = [
            [
                'name'    => 'fastchat',
                'title'   => '即时通信管理',
                'icon'    => 'fa fa-comments',
                'sublist' => [
                    [
                        'name'    => 'fastchat/pushmessage',
                        'title'   => '推送消息',
                        'icon'    => 'fa fa-circle-o',
                        'sublist' => [
                            ['name' => 'fastchat/pushmessage/index', 'title' => '推送消息']
                        ]
                    ],
                    [
                        'name'    => 'fastchat/serviceuser',
                        'title'   => '服务账号管理',
                        'icon'    => 'fa fa-circle-o',
                        'sublist' => [
                            ['name' => 'fastchat/serviceuser/index', 'title' => '查看'],
                            ['name' => 'fastchat/serviceuser/add', 'title' => '添加'],
                            ['name' => 'fastchat/serviceuser/edit', 'title' => '编辑'],
                            ['name' => 'fastchat/serviceuser/del', 'title' => '删除'],
                            ['name' => 'fastchat/serviceuser/multi', 'title' => '批量更新'],
                            ['name' => 'fastchat/serviceuser/recyclebin', 'title' => '回收站'],
                            ['name' => 'fastchat/serviceuser/destroy', 'title' => '真实删除'],
                            ['name' => 'fastchat/serviceuser/restore', 'title' => '还原'],
                        ]
                    ],
                ]
            ]
        ];
        Menu::create($menu);

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        // 删除命令
        $command_file = APP_PATH . 'command.php';
        $command_item = 'addons\fastchat\library\GatewayWorker\start';

        if (is_file($command_file)) {
            $fullConfigArr = include $command_file;
        }

        $command_existing = false;

        foreach ($fullConfigArr as $key => $value) {
            if ($value == $command_item) {
                $command_existing = true;
                unset($fullConfigArr[$key]);
            }
        }

        if ($command_existing) {
            if (is_really_writable($command_file)) {
                if ($handle = fopen($command_file, 'w')) {
                    fwrite($handle, "<?php\n\n" . "return " . var_export($fullConfigArr, true) . ";\n");
                    fclose($handle);
                }
            }
        }

        Menu::delete('fastchat');
        return true;
    }

    /**
     * 注册命令
     */
    public function appInit()
    {
        if (request()->isCli()) {
            Console::addDefaultCommands([
                'addons\fastchat\commands\WorkerStart',
                'addons\fastchat\commands\WorkerStartForWin',
            ]);
        }
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {

        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {

        return true;
    }
}
